package com.mz.mattress.admin.api.merchant;

import com.mz.mattress.admin.api.req.MerchantApplyReq;
import com.mz.mattress.admin.api.resp.MerchantSum;
import com.mz.mattress.admin.api.resp.MerchantVo;
import com.mz.mattress.admin.common.Holder;
import com.mz.mattress.admin.service.MerchantService;
import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Merchant;
import com.mz.mattress.api.jwt.MerchantJwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//商家端
@Slf4j
@RestController
@RequestMapping(Constants.MERCHANT_PATH + "merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    @GetMapping(value = "/get")
    public Result<Merchant> get() {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        Integer typeId = userJwt.getTypeId();
        log.info("typeId=" + typeId);
        return merchantService.getById(typeId);
    }

    @GetMapping(value = "/headSum")
    public Result<MerchantSum> headSum(@RequestParam Integer merchantId) {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        return merchantService.headSum(merchantId, userJwt.getMemberMerId(), null);
    }

    @GetMapping(value = "/merchantSum")
    public Result<MerchantSum> merchantSum(@RequestParam Integer typeId, @RequestParam(required = false) Integer days) {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        return merchantService.merchantSum(typeId, userJwt.getMemberMerId(), days);
    }

    @GetMapping(value = "/list")
    public Result<List<MerchantVo>> list() {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        Long memberMerId = userJwt.getMemberMerId();
        log.info("typeId={},memberId={}", userJwt.getTypeId(), memberMerId);
        return merchantService.list(memberMerId);
    }

    @GetMapping(value = "/applyList")
    public Result<List<MerchantVo>> applyList() {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        Long memberMerId = userJwt.getMemberMerId();
        log.info("memberId={}", memberMerId);
        return merchantService.applyList(memberMerId);
    }

    @GetMapping(value = "/{id}")
    public Result<Merchant> detail(@PathVariable Integer id) {
        return merchantService.getById(id);
    }

    @PostMapping("/submit")
    public Result submit(@RequestBody MerchantApplyReq req) {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        req.setMobile(userJwt.getMobile());
        req.setMemberId(userJwt.getMemberMerId());
        return merchantService.submit(req);
    }

    @PutMapping
    public Result update(@RequestBody MerchantApplyReq req) {
        return merchantService.updateById(req);
    }

    @GetMapping(value = "/resetCanUse")
    public Result resetCanUse() {
        MerchantJwt userJwt = Holder.MERCHANT_JWT_THREAD_LOCAL.get();
        Integer typeId = userJwt.getTypeId();
        log.info("typeId=" + typeId);
        return merchantService.resetCanUse(typeId);
    }

}
